package com.woniu.tms.exam.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.ClassesExample;
import com.woniu.tms.entity.Course;
import com.woniu.tms.entity.CourseExample;
import com.woniu.tms.entity.Exam;
import com.woniu.tms.entity.ExamExample;
import com.woniu.tms.entity.Role;
import com.woniu.tms.entity.RoleExample;
import com.woniu.tms.entity.Room;
import com.woniu.tms.entity.RoomExample;
import com.woniu.tms.entity.Score;
import com.woniu.tms.entity.ScoreExample;
import com.woniu.tms.entity.User;
import com.woniu.tms.entity.UserExample;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.entity.UserRoleExample;
import com.woniu.tms.entity.ExamExample.Criteria;
import com.woniu.tms.exam.service.ExamService;
import com.woniu.tms.mapper.ClassesMapper;
import com.woniu.tms.mapper.CourseMapper;
import com.woniu.tms.mapper.ExamMapper;
import com.woniu.tms.mapper.RoleMapper;
import com.woniu.tms.mapper.RoomMapper;
import com.woniu.tms.mapper.ScoreMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;
/**
 * @author Leo
 *
 */
@Service
public class ExamServiceImpl implements ExamService {
	@Autowired
	ExamMapper examMapper;
	
	@Autowired
	ClassesMapper classesMapper;
	
	@Autowired
	CourseMapper courseMapper;
	
	@Autowired
	UserRoleMapper userRoleMapper;	
	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	RoomMapper roomMapper;
	
	@Autowired
	ScoreMapper scoreMapper;
	
	@Autowired
	UserMapper userMapper;
	
	
	
	/**
	 *exam
	 */
	@Override
	public List<Exam> findExamByClasses(Classes classes) {
		// TODO Auto-generated method stub
		ExamExample examExample=new ExamExample();
		Criteria criteria=examExample.createCriteria();
		criteria.andClassidEqualTo(classes.getCid());
		List<Exam> examList=examMapper.selectByExample(examExample);
		return examList;
	}

	@Override
	public List<Exam> findExamByClassesList(List<Classes> classesList) {
		// TODO Auto-generated method stub
		ExamExample examExample=new ExamExample();
		Criteria criteria=examExample.createCriteria();
		List<Integer> classesIdList=new ArrayList<>();
		for (int i = 0; i < classesList.size(); i++) {
			classesIdList.add(classesList.get(i).getCid());
		}
		criteria.andClassidIn(classesIdList);
		List<Exam> examList=examMapper.selectByExample(examExample);
		return examList;
	}

	@Override
	public List<Exam> findExamAll() {
		// TODO Auto-generated method stub
		ExamExample examExample=new ExamExample();
		Criteria criteria=examExample.createCriteria();
		criteria.andClassidIsNotNull();
		List<Exam> examList=examMapper.selectByExample(examExample);
		return examList;
	}
	
	@Override
	public void addExam(Exam exam) {
		// TODO Auto-generated method stub
		examMapper.insert(exam);
	}
	
	@Override
	public void deleteByEid(Integer eid) {
		// TODO Auto-generated method stub
		examMapper.deleteByPrimaryKey(eid);
	}
	
	@Override
	public List<Exam> findExamByClassIdList(List<Integer> classIdList) {
		// TODO Auto-generated method stub
		
		ExamExample examExample=new ExamExample();
		Criteria criteria=examExample.createCriteria();
		criteria.andClassidIn(classIdList);
		List<Exam> examList=examMapper.selectByExample(examExample);
		return examList;
	}
	
	@Override
	public List<Integer> findEidByDaterange(String daterange1, String daterange2) throws ParseException {
		// TODO Auto-generated method stub
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		 Date date1 = sdf.parse(daterange1);
		 Date date2 = sdf.parse(daterange2);
		ExamExample examExample=new ExamExample();
		Criteria criteria=examExample.createCriteria();
		List<Integer> eidList=new ArrayList<Integer>();		
		criteria.andEndtimeBetween(date1, date2);
		List<Exam> examList=examMapper.selectByExample(examExample);
		for (Exam exam : examList) {
			eidList.add(exam.getEid());
		}
		
		return eidList;
	}

	@Override
	public Exam findExamByEid(Integer eid) {
		// TODO Auto-generated method stub		
		ExamExample examExample=new ExamExample();
		Exam exam=examMapper.selectByPrimaryKey(eid);
		return exam;
	}

	@Override
	public void updateExam(Exam exam) {
		// TODO Auto-generated method stub
		examMapper.insert(exam);
	}

	
	
	
	

	/**
	 *classes
	 */
	@Override
	public Classes findClassByClassId(Integer classid) {
		// TODO Auto-generated method stub
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria=classesExample.createCriteria();
		criteria.andCidEqualTo(classid);
		Classes classes=(Classes)classesMapper.selectByExample(classesExample);
		return classes;
	}

	@Override
	public List<Classes> findClassByTeacherId(Integer teacherid) {
		// TODO Auto-generated method stub
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria=classesExample.createCriteria();
		criteria.andTeacheridEqualTo(teacherid);
		List<Classes> classesList=(List<Classes>)classesMapper.selectByExample(classesExample);
		return classesList;
	}

	@Override
	public List<Classes> findClassAll() {
		// TODO Auto-generated method stub
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria=classesExample.createCriteria();
		criteria.andCidIsNotNull();
		List<Classes> classesList=(List<Classes>)classesMapper.selectByExample(classesExample);
		return classesList;
	}
	
	@Override
	public List<Integer> findCidByLikeCname(String cname) {
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria = classesExample.createCriteria();
		criteria.andCnameLike("%"+cname+"%");
		List<Classes> classesList =classesMapper.selectByExample(classesExample);
		List<Integer> classIdList = new ArrayList<Integer>();
		for (Classes classes : classesList) {
			classIdList.add(classes.getCid());
		}
		return classIdList;
	}
	
	@Override
	public List<Integer> findCidByTeacherId(Integer i) {
		// TODO Auto-generated method stub
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria = classesExample.createCriteria();
		criteria.andTeacheridEqualTo(i);
		List<Classes> classList=classesMapper.selectByExample(classesExample);
		List<Integer> cidList=new ArrayList<Integer>();
		for (Classes classes : classList) {
			cidList.add(classes.getCid());
		}
		return cidList;
	}
	

	@Override
	public List<Integer> findCidByClassManagerId(Integer i) {
		// TODO Auto-generated method stub
		ClassesExample classesExample=new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria = classesExample.createCriteria();
		criteria.andClassmanageridEqualTo(i);
		List<Classes> classList=classesMapper.selectByExample(classesExample);
		List<Integer> cidList=new ArrayList<Integer>();
		for (Classes classes : classList) {
			cidList.add(classes.getCid());
		}
		return cidList;
	}
	
	
	
	
	
	/**
	 *course
	 */
	@Override
	public List<Course> findCourseAll() {
		// TODO Auto-generated method stub
		CourseExample courseExample=new CourseExample();
		com.woniu.tms.entity.CourseExample.Criteria criteria=courseExample.createCriteria();
		criteria.andCourseidIsNotNull();
		List<Course> courseList=courseMapper.selectByExample(courseExample);
		return courseList;
	}
	
	/**
	 *role
	 */
	@Override
	public Role findRoleByUser(User loginUser) {
		// TODO Auto-generated method stub
		UserRoleExample userRoleExample=new UserRoleExample();
		RoleExample roleExample=new RoleExample();
		com.woniu.tms.entity.UserRoleExample.Criteria createria=userRoleExample.createCriteria();
		createria.andUidEqualTo(loginUser.getUid());
		UserRole userRole=(UserRole)userRoleMapper.selectByExample(userRoleExample);
		com.woniu.tms.entity.RoleExample.Criteria createria2=roleExample.createCriteria();
		createria2.andRidEqualTo(userRole.getRid());
		Role role=(Role)roleMapper.selectByExample(roleExample);
		return role;
	}
	
	
	/**
	 *room
	 */
	@Override
	public List<Room> findRoomAll() {
		// TODO Auto-generated method stub
		RoomExample roomExample=new RoomExample();
		com.woniu.tms.entity.RoomExample.Criteria criteria=roomExample.createCriteria();
		criteria.andRidIsNotNull();
		List<Room> roomList=roomMapper.selectByExample(roomExample);
		return roomList;
	}
	
	
	/**
	 *score
	 */
	@Override
	public List<Score> findScoreByStudentId(Integer uid) {
		// TODO Auto-generated method stub
		ScoreExample scoreExample=new ScoreExample();
		com.woniu.tms.entity.ScoreExample.Criteria criteria=scoreExample.createCriteria();
		criteria.andStudentidEqualTo(uid);
		List<Score> scoreList=scoreMapper.selectByExample(scoreExample);
		return scoreList;
	}
	@Override
	public List<Score> findScoreByExamList(List<Exam> examList) {
		// TODO Auto-generated method stub
		ScoreExample scoreExample=new ScoreExample();
		com.woniu.tms.entity.ScoreExample.Criteria criteria=scoreExample.createCriteria();
		List<Integer> examIdList=new ArrayList<>();
		for (int i = 0; i < examList.size(); i++) {
			examIdList.add(examList.get(i).getClassid());
		}
		criteria.andExamidIn(examIdList);
		List<Score> scoreList=scoreMapper.selectByExample(scoreExample);
		return scoreList;
	}
	@Override
	public List<Score> findScoreAll() {
		// TODO Auto-generated method stub
		ScoreExample scoreExample=new ScoreExample();
		com.woniu.tms.entity.ScoreExample.Criteria criteria=scoreExample.createCriteria();
		criteria.andSidIsNotNull();
		List<Score> scoreList=scoreMapper.selectByExample(scoreExample);
		return scoreList;
	}
	@Override
	public Score findScoreBysid(Integer sid) {

		Score score=scoreMapper.selectByPrimaryKey(sid);
		return score;
	}
	@Override
	public void editScore(Score score) {
		// TODO Auto-generated method stub
		scoreMapper.updateByPrimaryKey(score);
	}
	@Override
	public void deleScoreBySid(Integer sid) {
		// TODO Auto-generated method stub
		scoreMapper.deleteByPrimaryKey(sid);
	}
	@Override
	public void addScore(Score score) {
		// TODO Auto-generated method stub
		scoreMapper.insert(score);
	}
	@Override
	public List<Score> findScoreByEidListAndSidList(List<Integer> sidList,List<Integer> eidList) {
		// TODO Auto-generated method stub
		ScoreExample scoreExample=new ScoreExample();
		com.woniu.tms.entity.ScoreExample.Criteria criteria=scoreExample.createCriteria();
		criteria.andSidIn(sidList);
		com.woniu.tms.entity.ScoreExample.Criteria criteria2=scoreExample.createCriteria();
		criteria2.andExamidIn(eidList);
		List<Score> scoreList=scoreMapper.selectByExample(scoreExample);
		return scoreList;		

	}
	
	@Override
	public List<Score> findScoreByStudentIdList(List<Integer> studentIdList) {
		// TODO Auto-generated method stub
		ScoreExample scoreExample=new ScoreExample();
		com.woniu.tms.entity.ScoreExample.Criteria criteria=scoreExample.createCriteria();
		criteria.andStudentidIn(studentIdList);
		List<Score> scoreList=scoreMapper.selectByExample(scoreExample);
		return scoreList;
	}
	

	
	
	
	

	
	/**
	 *user
	 */
	@Override
	public List<User> findUserAll() {
		// TODO Auto-generated method stub
		UserExample userExample=new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria=userExample.createCriteria();
		criteria.andUidIsNotNull();
		List<User> userList=userMapper.selectByExample(userExample);
		return userList;
	}

	@Override
	public List<Integer> findUidByLikeUname(String uname) {
		// TODO Auto-generated method stub
		UserExample userExample=new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria=userExample.createCriteria();
		criteria.andUnameLike("%"+uname+"%");
		List<User> userList=userMapper.selectByExample(userExample);
		List<Integer> uidList=new ArrayList<Integer>();	
		for (User user : userList) {
			uidList.add(user.getUid());
		}
		return uidList;
	}
	
	@Override
	public List<Integer> findUidByClassIdList(List<Integer> cidList) {
		// TODO Auto-generated method stub
		UserExample userExample=new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria=userExample.createCriteria();
		criteria.andClassidIn(cidList);
		List<User> userList=userMapper.selectByExample(userExample);
		List<Integer> userIdList=new ArrayList<Integer>();
		for (User user : userList) {
			userIdList.add(user.getUid());
		}
			
		return userIdList;
	}

	@Override
	public List<Exam> findExamByLikeName(String name) {
		// TODO Auto-generated method stub

		ExamExample examExmaple=new ExamExample();
		Criteria criteria=examExmaple.createCriteria();
		criteria.andNameLike("%"+name+"%"); 
		List<Exam> examList=examMapper.selectByExample(examExmaple);
		return examList;
	}

	@Override
	public List<Exam> findExamByLikeNameAndCidListAndDaterange(String name,List<Integer> cidList,String daterange1, String daterange2) throws ParseException {
		// TODO Auto-generated method stub
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd ");
		 Date date1 = sdf.parse(daterange1);
		 Date date2 = sdf.parse(daterange2);
		ExamExample examExmaple=new ExamExample();
		Criteria criteria=examExmaple.createCriteria();
		criteria.andNameLike("%"+name+"%"); 
		Criteria criteria2=examExmaple.createCriteria();
		criteria2.andStarttimeBetween(date1, date2);
		if(!cidList.equals(null)) {
			Criteria criteria3=examExmaple.createCriteria();
			criteria2.andClassidIn(cidList);
		}
		List<Exam> examList=examMapper.selectByExample(examExmaple);
		return null;
	}

	

	
	
	







	
	
	




	





	

	
	
	
	
	


}
